package com.st.xtmj;

import com.google.gson.Gson;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


public class App {
    public static final Logger logger = LogManager.getLogger(App.class);
    public static Gson gson = new Gson();

    public static void main(String[] args) {
        try {
            if (args == null) {
                logger.info("参数错误！！！");
                return;
            }
            if (args.length < 2) {
                logger.info("参数错误！！！");
                return;
            }
            logger.info(gson.toJson(args));
            String fileName = args[0];
            String targetFolder = args[1];
            if(!(targetFolder.endsWith("/")||targetFolder.endsWith("\\"))) {
                targetFolder=targetFolder+File.separator;
            }
            logger.info("=================================开始读取文件===============================================");
            String jsonStr = FileUtils.readFileToString(new File(fileName), "utf-8");
            HashMap<String, Object> msp = gson.fromJson(jsonStr, HashMap.class);
            List<Map<String, Object>> entities = (List<Map<String, Object>>) msp.get("Entities");
            List<Map<String, Object>> categories = (List<Map<String, Object>>) msp.get("Categories");
            List<Map<String, Object>> types = (List<Map<String, Object>>) msp.get("Types");
            List<Map<String, Object>> floors = (List<Map<String, Object>>) msp.get("Floors");
            Map<Object, Object> categoriesMap = new HashMap<>();
            categories.forEach(category -> {
                categoriesMap.put(category.get("Id"), category.get("Name"));
            });
            Map<Object, Object> floorsMap = new HashMap<>();
            floors.forEach(floor -> {
                floorsMap.put(floor.get("Id"), floor.get("Name"));

            });
            logger.info("=================================开始把entities写入文件=====================================");
            String finalTargetFolder = targetFolder;
            entities.forEach(entity -> {
                //设置分类名称
                entity.put("CategoryName", categoriesMap.get(entity.get("CategoryID")));
                try {
                    FileUtils.writeStringToFile(new File(finalTargetFolder + entity.get("IfcGuid") + ".json"), gson.toJson(entity), "utf-8");
                } catch (IOException e) {
                    logger.info("entities:", e.getMessage());
                }
            });
            logger.info("=================================entities写入完成==========================================");
            logger.info("=================================开始把types写入文件========================================");
            types.forEach(entity -> {
                //设置分类名称
                entity.put("CategoryName", categoriesMap.get(entity.get("CategoryID")));
                entity.put("floorName", categoriesMap.get(entity.get("CategoryID")));
                try {
                    FileUtils.writeStringToFile(new File(finalTargetFolder + entity.get("IfcGuid") + ".json"), gson.toJson(entity), "utf-8");
                } catch (IOException e) {
                    logger.info("types:", e.getMessage());
                }
            });
            logger.info("=================================types写入完成=============================================");
            logger.info("=================================文件提取成功===============================================");
            logger.info("=================================输入任意键结束程序===============================================");
            System.in.read();
        } catch (IOException e) {
            logger.info(e.getMessage());
        }
    }
}


